import os
import configparser
import sys
# 获取当前脚本所在目录
if getattr(sys, 'frozen', False):
    script_path = os.path.abspath(sys.executable)
    script_dir = os.path.dirname(script_path)
    print('***当前程序以.exe模式运行***')
    print('\n')
else:
    script_path = os.path.abspath(__file__)
    script_dir = os.path.dirname(script_path)
    print('***当前程序以.py模式运行***')
    print('\n')
if hasattr(sys, '_MEIPASS'):
    script_dir = os.path.join(sys._MEIPASS, script_dir.lstrip(os.sep))
config = configparser.ConfigParser()

config['mysqld'] = {
    'port': '3306',
    'basedir': 'D:\\CNKI\\TPIWEB\\mysql',
    'datadir': 'D:\\CNKI\\TPIWEB\\mysql\\data',
    'max_connections': '500',
    'max_connect_errors': '10',
    'default-storage-engine': 'INNODB',
    'default_authentication_plugin': 'mysql_native_password',
    'innodb_flush_method': 'normal',
    'sql_mode': 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
}

config['mysql'] = {
    'default-character-set': 'utf8mb4'
}

config['client'] = {
    'port': '3306',
    'default-character-set': 'utf8mb4'
}

# 将配置信息写入 my.ini 文件中
with open('my.ini', 'w') as configfile:
    config.write(configfile)
    print('1、已创建my.ini文件')

config.read('my.ini')
# 获取 'mysqld' 部分的 'basedir' 值
basedir = config.get('mysqld', 'basedir')
datadir = config.get('mysqld', 'datadir')
# 修改 'basedir' 的值
base_dir = script_dir
data_dir = script_dir + '\data'
config.set('mysqld', 'basedir', base_dir)
print('2、将my.ini中basedir的值修改为：' + base_dir)
config.set('mysqld', 'datadir', data_dir)
print('3、将my.ini中datadir的值修改为：' + data_dir)
# 将修改后的配置写入文件
with open('my.ini', 'w') as configfile:
    config.write(configfile)
print('\n')
input('操作成功！请按回车键以退出此程序。')
